package com.ruoyi.dataserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.dataserver.model.line.LINE;
import com.ruoyi.dataserver.service.IDownSteamAnalyzeService;
import com.ruoyi.dataserver.service.ISearchTableService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ProjectName
 * @Package com.lab.dataserver.service.impl
 * @ClassName TopologyAnalyzeServceImpl
 * @Description 下游逻辑层
 * @Company lab
 * @Author wangw
 * @Date 2020/7/9 0009 11:55
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Slf4j
@Component
public class DownStreamAnalyzeServceImpl implements IDownSteamAnalyzeService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    public ISearchTableService tableService;

    List<String> us_idList = new ArrayList<String>();
    List<String> nodeIDList = new ArrayList<String>();



    public Map downStreamAnalyze(String dm, String usidParm) {
        List<LINE> list = new ArrayList();
        String tableNameString = tableService.searchLineTable(dm);
        String sql = "select * from \""+tableNameString+"\" where US_ID ='"+usidParm+"'";
        LINE line = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<LINE>(LINE.class));
        getOneDownstreamLine(line,list);
        JSONObject result = new JSONObject();
        result.put("result", list);
        return result;
    }
    public void getOneDownstreamLine(LINE zhswGXLine,List<LINE> list){
        //if(list.size() < 200){
            if("0".equals(zhswGXLine.getLX())){
                StringBuffer sb = new StringBuffer();
                String linetable = tableService.searchLineTable(zhswGXLine.getDM());
                String sql = sb.append("select * from \"").append(linetable)
                        .append("\" where lx = '0' ")
                        .append(" and qddh ='").append(zhswGXLine.getZDDH()).append("'")
                        .toString();
                RowMapper<LINE> rowMapper1  = new BeanPropertyRowMapper<LINE>(LINE.class);
                List<LINE> lineList = jdbcTemplate.query(sql,rowMapper1);
                if(lineList.size() > 0){
                    for(LINE line : lineList){
                        list.add(line);
                        getOneDownstreamLine(line,list);
                    }
                }

                StringBuffer sb1 = new StringBuffer();
                String sql1 = sb1.append("select * from \"").append(linetable)
                        .append("\" where lx = '1' ")
                        .append(" and zddh ='").append(zhswGXLine.getZDDH()).append("'")
                        .toString();
                List<LINE> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
                if(lineList1.size() > 0){
                    for(LINE line : lineList1){
                        list.add(line);
                        getOneDownstreamLine(line, list);
                    }
                }
            }else{
                StringBuffer sb = new StringBuffer();
                String linetable = tableService.searchLineTable(zhswGXLine.getDM());
                String sql = sb.append("select * from \"").append(linetable)
                        .append("\" where lx = '0' ")
                        .append(" and qddh ='").append(zhswGXLine.getQDDH()).append("'")
                        .toString();
                RowMapper<LINE> rowMapper1  = new BeanPropertyRowMapper<LINE>(LINE.class);
                List<LINE> lineList = jdbcTemplate.query(sql,rowMapper1);
                if(lineList.size() > 0){
                    for(LINE line : lineList){
                        list.add(line);
                        getOneDownstreamLine(line, list);
                    }
                }

                StringBuffer sb1 = new StringBuffer();
                String sql1 = sb1.append("select * from \"").append(linetable)
                        .append("\" where lx = '1' ")
                        .append(" and zddh ='").append(zhswGXLine.getQDDH()).append("'")
                        .toString();
                List<LINE> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
                if(lineList1.size() > 0){
                    for(LINE line : lineList1){
                        list.add(line);
                        getOneDownstreamLine(line, list);
                    }
                }
            }
       // }

    }
    /** 
     * @Title 数据去重
     * @Description
     * @return
     * @author wangw
     * @date 2020/7/18 0018 11:12
    */
    public Map<String, List> build(Map<String,List> roadMap, String roadName, String SNODIDText, String ENODIDText){
        List list = roadMap.get(roadName);
        list.add(SNODIDText);
        list.add(ENODIDText);
        List<String> myList = (List<String>) list.stream().distinct().collect(Collectors.toList());
        myList.stream().map(point->{
            String[] pointArray = point.split("-");
            Double[] pointDouble = new Double[pointArray.length];
            pointDouble[0] = Double.valueOf(pointArray[0]);
            pointDouble[1] = Double.valueOf(pointArray[1]);
            return pointDouble;
        }).collect(Collectors.toList());

        roadMap.put(roadName,myList);
        return roadMap;
    }

    
    //确定起始输入点为起点还是终点
    public String ToQueryID(Map feature) {
        String a = "";
        String LX = String.valueOf(feature.get("LX"));
        if (LX.equals("0") || LX.equals("") || LX == "null")//由起点流向终点
        {
            a = feature.get("ENODID").toString();
        } else if (LX.equals("1")) {
            a = feature.get("SNODID").toString();
        }
        return a;
    }

    //获取满足查询条件 游标
    public List<Map<String, Object>> ToQueryFeatureCursor(String toQueryClause) {
        System.out.println("##########"+toQueryClause);
        List<Map<String, Object>> result = jdbcTemplate.queryForList(toQueryClause);
        return result;
    }

    /**
     * @Title 根据管线类型处理数据
     * @Description
     * @return
     * @author wangw
     * @date 2020/7/14 0014 11:34
     */
    public List disposeDataByType(String dm,String tableName,List userIdList){
        List list = new ArrayList();
        for (int i = 0; i < userIdList.size(); i++) {
            String sql = "select * from \""+tableName+"\" where US_ID ='"+userIdList.get(i).toString()+"'";
            
            LINE line = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<LINE>(LINE.class));
            list.add(line);
            
        }
        return list;
    }

}
